import pandas as pd
import os
from service.utils import rect_img


def paint_rect(data_path, image_path):
    record_data = pd.read_csv(data_path, index_col=0)
    all_step = record_data.shape[0]
    for run_step in range(0, record_data.shape[0]):
        click_delay = record_data.iloc[run_step, 0]
        press_type = record_data.iloc[run_step, 1]
        event_type = record_data.iloc[run_step, 2]
        input_data = eval(
            record_data.iloc[run_step, 3].replace("", ""))
        if event_type == 'mouse left up':
            next_up = run_step+2
            if next_up < all_step:
                next_press_type = record_data.iloc[next_up, 1]
                next_event_type = record_data.iloc[next_up, 2]
                next_input_data = eval(
                    record_data.iloc[next_up, 3].replace("", ""))
                if event_type == next_event_type and input_data == next_input_data:
                    run_step = next_up
            image_file = os.path.join(
                image_path, str(run_step+1) + '.png')
            roi = record_data.at[int(run_step+1), '比对区域']
            if not pd.isna(roi):
                rect_img(image_file, roi, 1)
        if event_type == 'mouse right up':
            image_file = os.path.join(
                image_path, str(run_step+1) + '.png')
            roi = record_data.at[int(run_step), '比对区域']
            if not pd.isna(roi):
                rect_img(image_file, roi, 1)
